﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.WebCore.LogExtend
{
    public static class LogExtension
    {
        /// <summary>
        /// 配置Log4Net
        /// </summary>
        /// <param name="builder"></param>
        public static void Log4Register(this WebApplicationBuilder builder)
        {
            //一、Log4net--第三方日志组件
            //1、nuget引入程序集 log4net +Microsoft.Extensions.Logging.Log4Net.AspNetCore
            //2、准备配置文件
            //3.让配置文件生效
            {
                try
                {
                    builder.Logging.AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Warning);  //忽略系统日志
                    builder.Logging.AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Warning);   //忽略系统日志
                    builder.Logging.AddLog4Net("ConfigFile/log4net.Config");
                    builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("出现异常：" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 配置Nlog
        /// </summary>
        /// <param name="builder"></param>
        public static void NLogRegister(this WebApplicationBuilder builder)
        {
            //一、Log4net--第三方日志组件
            //1、nuget引入程序集  NLog.Web.AspNetCore +  NLog
            //2、准备配置文件
            //3.让配置文件生效
            //4.如果要让Nlog支持数据库写入，Nuget还需要引入一个Nlog.Database 
            NLog.LogManager.Setup().LoadConfigurationFromFile("ConfigFile/NLog.config").GetCurrentClassLogger();
            builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
            builder.Host.UseNLog();

        }
    }
}
